home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Python / thread_os2.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  4.1 KB  |  191 lines

  1. /* This code implemented by cvale@netcom.com */
  2.  
  3. #define INCL_DOSPROCESS
  4. #define INCL_DOSSEMAPHORES
  5. #include "os2.h"
  6. #include "limits.h"
  7.  
  8. #include "process.h"
  9.  
  10. long PyThread_get_thread_ident(void);
  11.  
  12.  
  13. /*
  14.  * Initialization of the C package, should not be needed.
  15.  */
  16. static void PyThread__init_thread(void)
  17. {
  18. }
  19.  
  20. /*
  21.  * Thread support.
  22.  */
  23. int PyThread_start_new_thread(void (*func)(void *), void *arg)
  24. {
  25.   int aThread;
  26.   int success = 1;
  27.  
  28.   aThread = _beginthread(func,NULL,65536,arg);
  29.  
  30.   if( aThread == -1 ) {
  31.     success = 0;
  32.     fprintf(stderr,"aThread failed == %d",aThread);
  33.     dprintf(("_beginthread failed. return %ld\n", errno));
  34.   }
  35.  
  36.   return success;
  37. }
  38.  
  39. long PyThread_get_thread_ident(void)
  40. {
  41.   PPIB pib;
  42.   PTIB tib;
  43.  
  44.   if (!initialized)
  45.     PyThread_init_thread();
  46.         
  47.   DosGetInfoBlocks(&tib,&pib);
  48.   return tib->tib_ptib2->tib2_ultid;
  49. }
  50.  
  51. static void do_PyThread_exit_thread(int no_cleanup)
  52. {
  53.   dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
  54.   if (!initialized)
  55.     if (no_cleanup)
  56.       _exit(0);
  57.     else
  58.       exit(0);
  59.   _endthread();
  60. }
  61.  
  62. void PyThread_exit_thread(void)
  63. {
  64.   do_PyThread_exit_thread(0);
  65. }
  66.  
  67. void PyThread__exit_thread(void)
  68. {
  69.   do_PyThread_exit_thread(1);
  70. }
  71.  
  72. #ifndef NO_EXIT_PROG
  73. static void do_PyThread_exit_prog(int status, int no_cleanup)
  74. {
  75.   dprintf(("PyThread_exit_prog(%d) called\n", status));
  76.   if (!initialized)
  77.     if (no_cleanup)
  78.       _exit(status);
  79.     else
  80.       exit(status);
  81. }
  82.  
  83. void PyThread_exit_prog(int status)
  84. {
  85.   do_PyThread_exit_prog(status, 0);
  86. }
  87.  
  88. void PyThread__exit_prog _P1(int status)
  89. {
  90.   do_PyThread_exit_prog(status, 1);
  91. }
  92. #endif /* NO_EXIT_PROG */
  93.  
  94. /*
  95.  * Lock support. It has too be implemented as semaphores.
  96.  * I [Dag] tried to implement it with mutex but I could find a way to
  97.  * tell whether a thread already own the lock or not.
  98.  */
  99. PyThread_type_lock PyThread_allocate_lock(void)
  100. {
  101.   HMTX   aLock;
  102.   APIRET rc;
  103.  
  104.   dprintf(("PyThread_allocate_lock called\n"));
  105.   if (!initialized)
  106.     PyThread_init_thread();
  107.  
  108.   DosCreateMutexSem(NULL,  /* Sem name      */
  109.                     &aLock, /* the semaphone */
  110.                     0,     /* shared ?      */
  111.                     0);    /* initial state */  
  112.  
  113.   dprintf(("%ld: PyThread_allocate_lock() -> %lx\n", PyThread_get_thread_ident(), (long)aLock));
  114.  
  115.   return (PyThread_type_lock) aLock;
  116. }
  117.  
  118. void PyThread_free_lock(PyThread_type_lock aLock)
  119. {
  120.   dprintf(("%ld: PyThread_free_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock));
  121.  
  122.   DosCloseMutexSem((HMTX)aLock);
  123. }
  124.  
  125. /*
  126.  * Return 1 on success if the lock was acquired
  127.  *
  128.  * and 0 if the lock was not acquired. This means a 0 is returned
  129.  * if the lock has already been acquired by this thread!
  130.  */
  131. int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
  132. {
  133.   int   success = 1;
  134.   ULONG rc, count;
  135.   PID   pid = 0;
  136.   TID   tid = 0;
  137.  
  138.   dprintf(("%ld: PyThread_acquire_lock(%lx, %d) called\n", PyThread_get_thread_ident(),
  139.            (long)aLock, waitflag));
  140.  
  141.   DosQueryMutexSem((HMTX)aLock,&pid,&tid,&count);
  142.   if( tid == PyThread_get_thread_ident() ) { /* if we own this lock */
  143.     success = 0;
  144.   } else {
  145.     rc = DosRequestMutexSem((HMTX) aLock,
  146.                             (waitflag == 1 ? SEM_INDEFINITE_WAIT : 0));
  147.     
  148.     if( rc != 0) {
  149.       success = 0;                /* We failed */
  150.     }
  151.   }
  152.  
  153.   dprintf(("%ld: PyThread_acquire_lock(%lx, %d) -> %d\n",
  154.            PyThread_get_thread_ident(),(long)aLock, waitflag, success));
  155.  
  156.   return success;
  157. }
  158.  
  159. void PyThread_release_lock(PyThread_type_lock aLock)
  160. {
  161.   dprintf(("%ld: PyThread_release_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock));
  162.  
  163.   if ( DosReleaseMutexSem( (HMTX) aLock ) != 0 ) {
  164.     dprintf(("%ld: Could not PyThread_release_lock(%lx) error: %l\n",
  165.              PyThread_get_thread_ident(), (long)aLock, GetLastError()));
  166.   }
  167. }
  168.  
  169. /*
  170.  * Semaphore support.
  171.  */
  172. PyThread_type_sema PyThread_allocate_sema(int value)
  173. {
  174.   return (PyThread_type_sema) 0;
  175. }
  176.  
  177. void PyThread_free_sema(PyThread_type_sema aSemaphore)
  178. {
  179.  
  180. }
  181.  
  182. int PyThread_down_sema(PyThread_type_sema aSemaphore, int waitflag)
  183. {
  184.   return -1;
  185. }
  186.  
  187. void PyThread_up_sema(PyThread_type_sema aSemaphore)
  188. {
  189.   dprintf(("%ld: PyThread_up_sema(%lx)\n", PyThread_get_thread_ident(), (long)aSemaphore));
  190. }
  191.